package cn.mumzone.pos.ui.widgets;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.DailySummaryEntity;


/**
 * Created by qiuzhenhuan on 2019/3/28.
 */

public class DailySummaryView extends LinearLayout {

    public static final int TYPE_TITLE = 1;
    public static final int TYPE_BODY = 2;

    RelativeLayout clTitle;
    RelativeLayout clBody;

    TextView tvDailyTitle;
    TextView tvDailyPrice;
    View viewTitleLine;
    ImageView ivTitleTip;

    TextView tvBodyTitle;
    TextView tvBodyPrice;
    View viewBodyLine;
    ImageView ivBodyTip;

    private int tip;

    private Context context;


    public DailySummaryView(Context context) {
        this(context, null);
    }

    public DailySummaryView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public DailySummaryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.item_daily_list, this);
        tvDailyTitle = findViewById(R.id.tv_daily_title);
        tvDailyPrice = findViewById(R.id.tv_daily_price);
        viewTitleLine = findViewById(R.id.view_title_line);
        ivTitleTip = findViewById(R.id.iv_title_tip);

        tvBodyTitle = findViewById(R.id.tv_body_title);
        tvBodyPrice = findViewById(R.id.tv_body_price);
        viewBodyLine = findViewById(R.id.view_body_line);
        ivBodyTip = findViewById(R.id.iv_body_tip);

        clTitle = findViewById(R.id.cl_title);
        clBody = findViewById(R.id.cl_body);
    }


    public void bindData(DailySummaryEntity entity, boolean isLastItem) {
        clTitle.setVisibility(entity.getType() == TYPE_TITLE ? View.VISIBLE : GONE);
        clBody.setVisibility(entity.getType() == TYPE_BODY ? View.VISIBLE : GONE);
        tip = entity.getTip();

        tvDailyTitle.setText(entity.getTitle());
        tvDailyPrice.setText(String.format("￥%s", StringUtils.getFormattedMoney(entity.getPrice())));
        viewTitleLine.setVisibility(!isLastItem ? View.VISIBLE : View.GONE);
        ivTitleTip.setVisibility(tip != -1 ? View.VISIBLE : View.GONE);
        ivTitleTip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemSelected(v, context.getString(tip));
                }
            }
        });

        tvBodyTitle.setText(String.format("- %s", entity.getTitle()));
        tvBodyPrice.setText(String.format("￥%s", StringUtils.getFormattedMoney(entity.getPrice())));
        viewBodyLine.setVisibility(!isLastItem ? View.VISIBLE : View.GONE);
        ivBodyTip.setVisibility(tip != -1 ? View.VISIBLE : View.GONE);
        ivBodyTip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemSelected(v, context.getString(tip));
                }
            }
        });
    }


    public interface OnItemTipClickedListener {
        void onItemSelected(View view, String tip);
    }

    private OnItemTipClickedListener mListener;

    public void setOnItemTipClickedListener(OnItemTipClickedListener mListener) {
        this.mListener = mListener;
    }
}
